﻿using System;
using System.Windows;
using System.Linq;
#if WPF
using System.Windows.Media;
using System.Windows.Shapes;

#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.Foundation;
using Windows.UI.Xaml.Data;
#endif

namespace Eternal.DataVisualization
{
    public class DoughnutSeries : NonAxisType
    {
        public DoughnutSeries()
        {
            DefaultStyleKey = typeof(DoughnutSeries);
        }

        protected internal override void OnDrawSegment()
        {
            base.OnDrawSegment();
            SegmentInfos.Clear();
            SegmentPanel.Children.Clear();
            double dataCount = ActualXData.Count;
            var sum = ActualYData.Sum();
            var halfCircle = HalfCircle;
            var sa = halfCircle ? 90 * (Math.PI / 2) : 0;
            for (var i = 0; i < dataCount; i++)
            {
                var yValue = ActualYData[i];

                var ea = Math.Abs(yValue) * (Math.PI * (halfCircle ? 1 : 2) / sum) + sa;

                Point origin;
                var radius = 0d;
                if (halfCircle)
                {
                    origin = new Point(AvailableWidth / 2, AvailableHeight);
                    radius = Math.Min(AvailableHeight, AvailableWidth / 2);
                }
                else
                {
                    origin = new Point(AvailableWidth / 2, AvailableHeight / 2);
                    radius = Math.Min(AvailableHeight, AvailableWidth) / 2;
                }

                var innerRadius = 0.45d * radius;

                var pathFigure = new PathFigure();
                pathFigure.StartPoint = new Point(origin.X + radius * Math.Cos(sa), origin.Y + radius * Math.Sin(sa));

                var argOuterSegment = new ArcSegment
                {
                    Point = new Point(origin.X + radius * Math.Cos(ea), origin.Y + radius * Math.Sin(ea)),
                    Size = new Size(radius, radius),
                    RotationAngle = ea - sa,
                    IsLargeArc = ea - sa > Math.PI,
                    SweepDirection = SweepDirection.Clockwise
                };
                pathFigure.Segments.Add(argOuterSegment);

                var line = new LineSegment
                {
                    Point = new Point(origin.X + innerRadius * Math.Cos(ea), origin.Y + innerRadius * Math.Sin(ea))
                };

                pathFigure.Segments.Add(line);

                var argInnerSegment = new ArcSegment
                {
                    Point = new Point(origin.X + innerRadius * Math.Cos(sa), origin.Y + innerRadius * Math.Sin(sa)),
                    Size = new Size(innerRadius, innerRadius),
                    RotationAngle = ea - sa,
                    IsLargeArc = ea - sa > Math.PI,
                    SweepDirection = SweepDirection.Counterclockwise
                };

                pathFigure.Segments.Add(argInnerSegment);

                pathFigure.IsClosed = true;

                var geo = new PathGeometry();
                geo.Figures = new PathFigureCollection { pathFigure };

                var path = new Path { Fill = Area.GetBrush(i) };
                path.Data = geo;
                SegmentPanel.Children.Add(path);

                var info = new SegmentInfo();
                info.Value = new Point(i, yValue);
                var centerAngle = sa + (ea - sa) / 2;
                var labelRadius = (radius * LabelRadius) / 100;
                info.Point = new Point(origin.X + labelRadius * Math.Cos(centerAngle), origin.Y + labelRadius * Math.Sin(centerAngle));
                info.Series = this;
                if (Label != null)
                {
                    Label.Positions.Add(info);
                }

                sa += (ea - sa);
            }
        }
    }
}
